<?php
/**
 * Created by PhpStorm.
 * User: HarrisHuang
 * Date: 17/5/20
 * Time: 11:41
 */
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Articles;
use App\Models\ArticleGroup;
use Log;

class ArticlesController extends Controller
{

    /**
     * 保存文章
     * @param Request $request
     */
    public function save(Request $request, Articles $articles)
    {
        try {
            $articles->title = $request->input('title');
            $articles->subTitle = $request->input('subTitle');
            $articles->content = $request->input('content');
            $articles->linkUrl = $request->input('linkUrl');
            $articles->sort = $request->input('sort');
            $articles->cover = $request->input('cover');
            $articles->gid = $request->input('gid');
            $articles->status = 1;
            $articles->addTime = time();
            $articles->save();
        } catch (\Exception $e) {
            Log::error('save articles error ' . $e->getMessage());
            return $this->error();
        }
        return $this->success();
    }

    public function update(Request $request)
    {
        $id = (int)$request->input('id');
        if ($id <= 0) {
            return $this->error();
        }
        $articles = Articles::find($id);
        if(!$articles){
            return $this->error();
        }
        $articles->title = $request->input('title');
        $articles->subTitle = $request->input('subTitle');
        $articles->content = $request->input('content');
        $articles->linkUrl = $request->input('linkUrl');
        $articles->sort = $request->input('sort');
        $articles->cover = $request->input('cover');
        $articles->gid = $request->input('gid');
        $articles->save();
        return $this->success();
    }

    public function delete(Request $request)
    {
        $id = (int)$request->get('id');
        if ($id <= 0) {
            return $this->error();
        }
        $articles = Articles::find($id);
        if(!$articles) {
            return $this->error();
        }
        $articles->status = 4;
        $articles->save();
        return $this->success();
    }

    public function lists(Request $request)
    {
        $pageSize = $request->get('pageSize',10);
        $gid = $request->get('gid',0);
        $list = Articles::where('status',1)->where('gid', $gid)->orderBy('sort','desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function mobileLists(Request $request)
    {
        $pageSize = $request->get('pageSize',10);
        $gid = $request->get('gid',0);
        $list = Articles::where('status',1)->where('gid',$gid)->orderBy('sort','desc')->paginate($pageSize);
        $list = $list->toArray();
        $res = [
            'total' => $list['total'],
            'data' => $list['data'],
        ];
        return $this->success($res);
    }

    public function info(Request $request)
    {
        $id = (int)$request->get('id');
        if($id <= 0 ){
            return $this->error();
        }
        $info = Articles::where('status',1)->find($id);
        if(!$info){
            return $this->error();
        }
        return $this->success($info);
    }

    public function mobileInfo(Request $request)
    {
        $id = (int)$request->get('id');
        if($id <= 0 ){
            return $this->error();
        }
        $info = Articles::where('status',1)->find($id);
        if(!$info){
            return $this->error();
        }
        return $this->success($info);
    }

    /**
     * 修改文章分组
     */
    public function grouping(Request $request)
    {
        Articles::where('id', (int)$request->input('id'))
            ->update(['gid' => (int)$request->input('gid')]);
        return $this->success();
    }

    public function addGroup(Request $request, ArticleGroup $articleGroup)
    {
        $name = trim($request->input('name'));
        $articleGroup->name = $name;
        $articleGroup->save();
        return $this->success();
    }

    public function deleteGroup(Request $request, ArticleGroup $articleGroup)
    {
        $id = (int)$request->get('id');
        if($id <= 0){
            return $this->error();
        }
        $articleGroup::destroy($id);
        return $this->success();
    }

    public function groupList(Request $request, ArticleGroup $articleGroup)
    {
        $list = $articleGroup::orderBy('gid','desc')->get();
        return $this->success($list);
    }

}
